package com.aramis.mydytt.ui.common

import android.graphics.Color
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.appcompat.widget.AppCompatImageView
import androidx.core.content.ContextCompat
import com.aramis.mydytt.R
import com.aramis.mydytt.ui.base.BaseViewHolder
import com.aramis.mydytt.ui.base.SimpleRecyclerAdapter
import com.aramis.mydytt.ui.bean.MovieItemBean
import com.facebook.drawee.view.SimpleDraweeView

/**
 * Description: CommonMovieItemAdapter
 * @author lizhidan
 * Created: 2021/4/7
 */
class CommonMovieItemAdapter : SimpleRecyclerAdapter<MovieItemBean, CommonMovieItemAdapterViewHolder>() {
    private var selectMode = false

    fun openSelectMode() {
        selectMode = true
        notifyDataSetChanged()
    }

    fun closeSelectMode() {
        selectMode = false
        notifyDataSetChanged()
    }

    var onItemLongClickListener: ((MovieItemBean) -> Unit)? = null
    var onItemSelectClickListener: ((Int) -> Unit)? = null

    override fun onBindViewHolder(holder: CommonMovieItemAdapterViewHolder, position: Int) {
        holder.selectMode = selectMode
        holder.itemView.setOnLongClickListener {
            if (!selectMode && onItemLongClickListener != null) {
                val movieItemBean = dataList[position]
                movieItemBean.selected = true
                onItemLongClickListener?.invoke(movieItemBean)
            }
            false
        }
        holder.onItemSelectClickListener = onItemSelectClickListener
        super.onBindViewHolder(holder, position)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CommonMovieItemAdapterViewHolder {
        return CommonMovieItemAdapterViewHolder(parent, onItemClickListener)
    }
}

class CommonMovieItemAdapterViewHolder(parent: ViewGroup, val onItemClickListener: ((MovieItemBean) -> Unit)?) :
    BaseViewHolder<MovieItemBean>(parent, R.layout.item_common) {
    //    private val rootLayout: ViewGroup = itemView.findViewById(R.id.layout_movie_item)
    private val movieCoverView: SimpleDraweeView = itemView.findViewById(R.id.image_common_item)
    private val movieTitleView: TextView = itemView.findViewById(R.id.text_common_item)
    private val movieTitleSecView: TextView = itemView.findViewById(R.id.text_common_item_second)
    private val scoreView: TextView = itemView.findViewById(R.id.text_common_item_score)
    private val selectImageView: AppCompatImageView = itemView.findViewById(R.id.image_select_common)
    var selectMode = false
    var onItemSelectClickListener: ((Int) -> Unit)? = null

    override fun bind(m: MovieItemBean, position: Int) {
        movieCoverView.setImageURI(m.path)
        movieTitleView.text = m.title
        itemView.setOnClickListener {
            if (selectMode) {
                m.selected = !m.selected
                onItemSelectClickListener?.invoke(position)
            } else {
                onItemClickListener?.invoke(m)
            }
        }
        movieTitleSecView.text = if (m.playlist?.title != null) m.playlist.title else m.playSubTitle
        scoreView.text = m.score ?: "0.0"
        if (selectMode) {
            selectImageView.visibility = View.VISIBLE
            if (m.selected) {
                selectImageView.setImageResource(R.drawable.ic_select_tap)
                selectImageView.setColorFilter(ContextCompat.getColor(selectImageView.context, R.color.color_select))
            } else {
                selectImageView.setImageResource(R.drawable.ic_select_nor)
                selectImageView.setColorFilter(Color.WHITE)
            }
        } else {
            m.selected = false
            selectImageView.visibility = View.GONE
        }
    }
}